package com.example.personalizededucationsystem.controller;

import com.example.personalizededucationsystem.model.User;
import com.example.personalizededucationsystem.service.AIAssistanceService;
import com.example.personalizededucationsystem.utils.IP_VALUE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.nio.file.AccessDeniedException;
import java.util.Map;

@CrossOrigin(origins = IP_VALUE.IP)
@RestController
@RequestMapping("/api/ai")
public class AIAssistanceController {

    private final AIAssistanceService aiAssistanceService;

    @Autowired
    public AIAssistanceController(AIAssistanceService aiAssistanceService) {
        this.aiAssistanceService = aiAssistanceService;
    }


    @GetMapping("/explanation/error/{errorId}")
    @PreAuthorize("hasRole('STUDENT')")
    public ResponseEntity<Map<String, String>> getExplanationForError(@PathVariable Long errorId, @AuthenticationPrincipal User currentUser) throws AccessDeniedException {
        return ResponseEntity.ok(Map.of("explanation", aiAssistanceService.getExplanationForError(errorId, currentUser)));
    }
}
